<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace model;

/**
 * Model odpowiedzialny za producenta
 *
 * @author Damian
 */
class TblManufacturer extends Model{
    /**
     *<p>ilość elementów danego producenta
     * @var @readwrite
     */
    public $itemCount;
    /**
     * @readwrite
     * @column
     * @primary
     * @type int
     * @lenght 11
     */
    public $ManufacturerID;
    const ID="ManufacturerID";
    /**
     * @readwrite
     * @column
     * @type varchar
     * @length 50
     * @validate required, max(50), min(3)
     */
    public $Name;
    const NAME="Name";
    /**
     * @readwrite
     * @column
     * @isObject
     * @type TblShop
     */
    public $ShopID;
    const SHOP_ID="ShopID";
    /**
     * @readwrite
     * @column
     * @type text
     */
    public $URL;
    const URL="URL";
    /**
     * @readwrite
     */
    public $PictureID;
    const PICTURE_ID="PictureID";
    /**
     * <p>sprawdza czy dany element jest w danym sklepie</p>
     * @param \model\TblShop $shop <p>sklep do sprawdzenia</p>
     * @return boolean
     */
    public function compare(TblShop &$shop){
        $myshop = TblShop::getModel()->getByPrimaryKey($this->ShopID);
        if (!$myshop)  {
            return false;
        }
        return $myshop->ShopID==$shop->ShopID;
    }
    public function generateURL(){
        $this->URL = str_replace(" ","_",strip_tags($this->Name));
    }
    /**
     * 
     * @param type $shopID <p>id sklepu w którym szukamy</p>
     * @return TblManufacturer[] <p>tablica producentów z danego sklepu</p>
     */
    public function getAllByShopID($shopID){
        $criteria = new Criteria();
        $criteria->from(\model\TblManufacturer::getModel()->getTable())->join(TblPicture::getModel())
                ->where(\model\TblManufacturer::SHOP_ID, "=", $shopID, TblManufacturer::getModel()->getTable());
        return $this->all($criteria);
    }
    public function getByURL($URL, $shopID){
        $criteria = new Criteria();
        $criteria->from(\model\TblManufacturer::getModel()->getTable())
               ->join(TblPicture::getModel())
                ->where(\model\TblManufacturer::URL, "=", $URL, TblManufacturer::getModel()->getTable())->where(\model\TblManufacturer::SHOP_ID, "=", $shopID, TblManufacturer::getModel()->getTable(), " and ");
        //echo $criteria->getSelectQuery();
        return $this->first($criteria);
    }
    
}
